import React, { useState, useEffect } from 'react';
import { Form, Button, Space } from 'antd';
import { goBack, componentsMap } from '@/utils/util';
import UploadList from '@/components/UploadList';
import API from '@/api/xjindex.js';
import { useModel } from 'umi';
import moment from 'moment';

const workDeploymentUnit_Edit = ({ history }) => {
  const [form] = Form.useForm();
  const { initialState } = useModel('@@initialState');
  const { username, orgId, orgName } = initialState.info;
  const TableList = [
    {
      label: '工作名称',
      name: 'workName',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '紧急程度',
      name: '_emergency',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '接收单位',
      name: 'receiverName',
      type: 'input',
      otherProps: {
        disabled: true,
      },
      isremark: true,
    },
    {
      label: '工作说明',
      name: 'adress',
      type: 'textarea',
      otherProps: {
        disabled: true,
      },
      isremark: true,
    },
  ];
  const TableList1 = [
    {
      label: '处置单位',
      name: '_orgName',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '处置时间',
      name: '_updateTime',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '处置人',
      name: '_username',
      type: 'input',
      otherProps: {
        disabled: true,
      },
    },
    {
      label: '处置说明',
      name: '_adress',
      type: 'textarea',
      isremark: true,
    },
  ];
  const uploadOptions = {
    fileTypes: [
      'application/doc',
      'application/pdf',
      'application/msword',
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
      'image/jpg',
      'image/jpeg',
      'image/png',
    ],
    maxSize: 50,
    maxNum: 3,
    accept: [
      '.doc,.docx.,.pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,.jpg,.jpeg,.png',
    ],
    acceptMessage: 'jpg/word/pdf',
  };
  const submitFn = (values) => {
    const params = {
      workDisposeFile: values.workDisposeFile,
      adress: values._adress,
      id: history.location.query.id,
      receiverId: orgId,
    };
    console.log(params);
    API.updateSecurityWork(params).then((res) => {
      if (res) {
        goBack();
      }
    });
  };
  function initList() {
    const _JJCD = {};
    API.dictItemList()
      .then((res) => {
        if (res) {
          res.t.map((item) => {
            if (item.dictCode == 'JJCD') {
              _JJCD[item.dictItemType] = item.dictItemName;
            }
          });
        }
      })
      .then(() => {
        API.showJjt(history.location.query).then((res) => {
          if (res) {
            form.setFieldsValue({
              ...res.t,
              _emergency: _JJCD[res.t.emergency],
              _orgName: orgName,
              _updateTime: moment().format('YYYY-MM-DD'),
              _username: username,
            });
          }
        });
      });
  }
  useEffect(() => {
    initList();
  }, []);
  return (
    <div className="main-x">
      <Form form={form} onFinish={submitFn}>
        <div className="base-title">工作部署信息</div>
        <div className="ant-form-inline form-x bgf">
          {TableList.map((item) => {
            return (
              <Form.Item
                key={item.name}
                label={item.label}
                name={item.name}
                className={item.isremark ? 'block' : ''}
              >
                {componentsMap(item.type, item.otherProps)}
              </Form.Item>
            );
          })}
          <Form.Item label="附件信息"></Form.Item>
        </div>
        <div className="base-title mt20">处置情况</div>
        <div className="ant-form-inline form-x bgf">
          {TableList1.map((item) => {
            return (
              <Form.Item
                key={item.name}
                label={item.label}
                name={item.name}
                className={item.isremark ? 'block' : ''}
              >
                {componentsMap(item.type, item.otherProps)}
              </Form.Item>
            );
          })}
          <Form.Item
            label="附件信息"
            extra="支持jpg/word/pdf格式,单个大小在50M以内,支持多个上传，最多上传3个附件"
            name="workDisposeFile"
            rules={[{ required: true, message: '请上传文件' }]}
          >
            <UploadList
              uploadUrl="/work/fileUpload"
              options={uploadOptions}
            ></UploadList>
          </Form.Item>
        </div>
        <div className="footer-x">
          <Space size={18}>
            <Button type="primary" htmlType="submit">
              提交
            </Button>
            <Button type="primary" className="cancel-btn" onClick={goBack}>
              返回
            </Button>
          </Space>
        </div>
      </Form>
    </div>
  );
};

export default workDeploymentUnit_Edit;
